//
//  AddNewInvoiceInfoVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/20.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "AddNewInvoiceInfoVC.h"
#import "OpenInvoiceInfoModel.h"
@interface AddNewInvoiceInfoVC ()
@property (nonatomic,strong) CJTextField *fieldInvoiceHeader;
@property (nonatomic,strong) CJTextField *fieldTaxNum;
@property (nonatomic,strong) CJTextField *fieldBankName;
@property (nonatomic,copy) NSString *strBankCode; // 银行code
@property (nonatomic,strong) CJTextField *fieldBankIDNum;
@property (nonatomic,strong) CJTextField *fieldRegisterAddr;
@property (nonatomic,strong) CJTextField *fieldRegisterPhone;
//@property (nonatomic,strong) CJTextField *fieldPartType;
@property (nonatomic,strong) CJImageView *imgVSelect;
@property (nonatomic,copy) NSString *strSelect; //0 1
//@property (nonatomic,strong) CJTextField *fieldOpneInvoiceType; //开票方式
//@property (nonatomic,strong) UIImageView *imgVLeftOpneInvoiceType; //开票方式左侧图标
//@property (nonatomic,strong) UIView *viewReceiver; //实际开票人view
//@property (nonatomic,strong) UIImageView *imgVLeftReceiver; //实际开票人左侧图标
//@property (nonatomic,strong) UIView *viewReceiverLine;
//@property (nonatomic,copy) NSString *strOpneInvoiceType;
//@property (nonatomic,copy) NSString *strReceiverUserCode; // 实际收票人编码
//@property (nonatomic,strong) CJTextField *fieldReceiverUser; //实际
@end
// 0 1 2
@implementation AddNewInvoiceInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.isChange) {
        [self setTitleName:@"修改开票信息"];
    }else{
        [self setTitleName:@"新增开票信息"];
    }
    [self setBackLeftButton:@""];
    
    [self createCommonAddAdressView];
    self.strSelect = @"0";
    self.strBankCode = @"";
//    self.strOpneInvoiceType = @"";
//    self.strReceiverUserCode = @"";
  
    if (self.isChange) {
        [self loadDataInvoiceDetail];
    }
    
    [self setFieldLimit];
}

- (void)loadDataInvoiceDetail{
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/invoiceMessage/%@",CJBaseUrl,self.strCode);
    WS(ws);
    NSDictionary *dic = [[NSDictionary alloc]init];
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dic success:^(id  _Nonnull responseObject) {
        
        DLog(@"++++++++%@",responseObject);
        NSError * error ;
        OpenInvoiceInfoModel *model = [[OpenInvoiceInfoModel alloc]initWithDictionary:responseObject error:&error];
        
//        NSDictionary *dicPar = @{@"invoiceTitle":self.fieldInvoiceHeader.text,
//                                 @"taxRegistrationNumber":self.fieldTaxNum.text,
//                                 @"bank":self.strBankCode,
//                                 @"accountNumber":self.fieldBankIDNum.text,
//                                 @"":self.fieldRegisterAddr.text,
//                                 @"":@"",
//                                 @"":self.strOpneInvoiceType,
//                                 @"receiverUserCode":self.strReceiverUserCode,
//                                 @"isInvoiceDefault":self.strSelect
//                            };
        
        ws.fieldInvoiceHeader.text = model.invoiceTitle;
        ws.fieldTaxNum.text = model.taxRegistrationNumber;
        self.strBankCode = model.bank;
        ws.fieldBankName.text = model.bankName;
        ws.fieldBankIDNum.text = model.accountNumber;
        self.fieldRegisterAddr.text = model.registeredAddress;
        self.fieldRegisterPhone.text = model.registeredFixedPhone;;
//        self.strOpneInvoiceType = CJStringWithFormat(@"%ld",(long)model.type);
//        self.fieldOpneInvoiceType.text = model.typeName;
        self.strSelect = CJStringWithFormat(@"%ld",(long)model.isInvoiceDefault);
//        self.strReceiverUserCode = model.receiverUserCode;
        if ([self.strSelect isEqualToString:@"1"]) {
            self.imgVSelect.image = [UIImage imageNamed:@"selected"];
        }else{
            self.imgVSelect.image = [UIImage imageNamed:@"zcj_unSelected"];
        }
        
//        if ([self.strOpneInvoiceType isEqualToString:@"0"]) {
//            self.viewReceiver.hidden = YES;
//            self.viewReceiverLine.hidden = YES;
//        }else{
//            self.viewReceiver.hidden = NO;
//            self.viewReceiverLine.hidden = YES;
//            self.fieldReceiverUser.text = model.receiverUserName;
//        }
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}

- (void)createCommonAddAdressView{
    CGRect rcFrame = CGRectMake(0, kTopHeight, [[UIScreen mainScreen] bounds].size.width, 0);
     FlexFrameView *header = [[FlexFrameView alloc] initWithFlex:@"AddNewInvoiceInfoView" Frame:rcFrame Owner:self];
     header.flexibleHeight = YES;
    
//    if (self.isChange) {
//        self.imgVLeftReceiver.hidden = YES;
//        self.imgVLeftOpneInvoiceType.hidden = YES;
//    }else{
//        self.imgVLeftReceiver.hidden = NO;
//        self.imgVLeftOpneInvoiceType.hidden = NO;
//    }
    
    [header layoutIfNeeded];
    [self.view addSubview:header];
    
}

- (void)btnSelectBankNameAction{
    
    [self.view endEditing:NO];
    WS(ws);
    [CJNetWork getSenGoodsDicsTypeinfo:CJGetSendgoodsBank Success:^(id  _Nullable responseObject) {
        DLog(@"======%@",responseObject);
//        NSArray *list= responseObject[@"dictList"];
        NSDictionary *dic = responseObject[0];
        NSArray *list = dic[@"dictList"];
        NSString *strTitle = dic[@"name"];
        NSMutableArray *arrCodes = [[NSMutableArray alloc]init];
        NSMutableArray *arrName = [[NSMutableArray alloc]init];

        [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *dic = (NSDictionary *)obj;
            [arrCodes addObject:dic[@"code"]];
            [arrName addObject:dic[@"name"]];
        }];
        [ws btnSelectTypeActionWithType:@"0" withCodeArr:arrCodes withNameArr:arrName withTitleName:strTitle];
    }];
}

- (void)btnSelectTypeActionWithType:(NSString *)strType withCodeArr:(NSMutableArray *)arrCode withNameArr:(NSMutableArray *)arrBame withTitleName:(NSString *)strTitle{
    
    [self.view endEditing:NO];
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]initWithPickerMode:BRStringPickerComponentSingle];
    stringPickerView.title = strTitle;
    stringPickerView.dataSourceArr = arrBame;
    WS(ws);
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        if (strType.intValue == 0) {
             ws.fieldBankName.text = resultModel.selectValue;
             ws.strBankCode = arrCode[resultModel.index];
        }
//        else if (strType.intValue == 1) {
//            ws.fieldOpneInvoiceType.text = resultModel.selectValue;
//            ws.strOpneInvoiceType = arrCode[resultModel.index];
//            if ([ws.strOpneInvoiceType isEqualToString:@"0"]) {
//                ws.viewReceiver.hidden = YES;
//                ws.viewReceiverLine.hidden = YES;
//                ws.strReceiverUserCode = @"";
//            }else{
//                ws.viewReceiver.hidden = NO;
//                ws.viewReceiverLine.hidden = NO;
//
//            }
//        }else if (strType.intValue == 3){
//            ws.fieldReceiverUser.text = resultModel.selectValue;
//            ws.strReceiverUserCode = arrCode[resultModel.index];
//        }
    };
    
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
    stringPickerView.pickerStyle = customStyle;
    [stringPickerView show];
}


//- (void)btnSelectOpenInvoiceAction{
//
//    if (self.isChange) {
//        return;
//    }
//    WS(ws);
//    [CJNetWork getSenGoodsDicsTypeinfo:CJGetSendgoodsBillingType Success:^(id  _Nullable responseObject) {
//        DLog(@"======%@",responseObject);
////        NSArray *list= responseObject[@"dictList"];
//        NSDictionary *dic = responseObject[0];
//        NSArray *list = dic[@"dictList"];
//        NSString *strTitle = dic[@"name"];
//        NSMutableArray *arrCodes = [[NSMutableArray alloc]init];
//        NSMutableArray *arrName = [[NSMutableArray alloc]init];
//
//        [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            NSDictionary *dic = (NSDictionary *)obj;
//            [arrCodes addObject:dic[@"code"]];
//            [arrName addObject:dic[@"name"]];
//        }];
//        [ws btnSelectTypeActionWithType:@"1" withCodeArr:arrCodes withNameArr:arrName withTitleName:strTitle];
//    }];
//}
//- (void)btnSelectReceiverUserAction{
//    if (self.isChange) {
//        return;
//    }
//    if ([CJRegular getIsStrEmpty:self.strOpneInvoiceType]) {
//        [CJAlert showStringDismissWithDelay:@"请选选择开票方式"];
//    }else{
//        if ([self.strOpneInvoiceType isEqualToString:@"0"]) {
//
//        }else {
//
//            NSString *strType = @"";
//            if ([self.strOpneInvoiceType isEqualToString:@"1"]) {
//                strType = @"4";
//            }else if ([self.strOpneInvoiceType isEqualToString:@"2"]) {
//                strType = @"6";
//            }
//
//            NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/shipment",CJBaseUrl);
//            NSDictionary *dicPar = @{@"staffType":strType,
//            };
//
//            WS(ws);
//
//            [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
//
//                DLog(@"++++++++%@",responseObject);
//
////                NSDictionary *dic = responseObject[0];
//                NSArray *list = responseObject[@"records"];
//
//                if (list.count == 0) {
//                    [CJAlert showStringDismissWithDelay:@"暂无实际开票人信息"];
//
//                }else{
//                    NSString *strTitle = @"选择实际开票人";
//                    NSMutableArray *arrCodes = [[NSMutableArray alloc]init];
//                    NSMutableArray *arrName = [[NSMutableArray alloc]init];
//
//                    [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                        NSDictionary *dic = (NSDictionary *)obj;
//                        [arrCodes addObject:dic[@"code"]];
//                        [arrName addObject:dic[@"userName"]];
//                    }];
//                    [ws btnSelectTypeActionWithType:@"3" withCodeArr:arrCodes withNameArr:arrName withTitleName:strTitle];
//                }
//
//            } failure:^(NSError * _Nonnull error) {
//                DLog(@"++++++++%@",error);
//
//            }];
//
//        }
//    }
//
//}
- (void)setOpenInvioceInfo{
//    [CJAlert showStringDismissWithDelay:@"隐私协议"];
    if ([self.strSelect isEqualToString:@"0"]) {
        self.strSelect = @"1";
        self.imgVSelect.image = [UIImage imageNamed:@"selected"];
    }else{
        self.strSelect = @"0";
        self.imgVSelect.image = [UIImage imageNamed:@"zcj_unSelected"];
    }
    
}
- (void)setFieldLimit{
    
    self.fieldInvoiceHeader.textFieldType = CJTextFieldStringTypeNone;
    self.fieldInvoiceHeader.maxLength = CJTextLengthMax;
    
    self.fieldTaxNum.textFieldType = CJTextFieldStringTypeNone;
    self.fieldTaxNum.maxLength = 128;
    
    self.fieldBankIDNum.textFieldType = CJTextFieldStringTypeNumber;
    self.fieldBankIDNum.maxLength = CJTextLengthThirtyTwo;
    
    self.fieldRegisterAddr.textFieldType = CJTextFieldStringTypeNone;
    self.fieldRegisterAddr.maxLength = 128;
    
    self.fieldRegisterPhone.textFieldType = CJTextFieldStringTypeNumber;
    self.fieldRegisterPhone.maxLength = CJTextLengthPhone;
}
- (void)btnSaveInvoiceInfo{
    
//    if ([CJRegular getIsBankCardNum:self.fieldBankIDNum.text]) {
//
//    }else{
//        [CJAlert showStringDismissWithDelay:@"基本开户行账号不符合要求"];
//        return;
//    }
    
//    if ([self.strOpneInvoiceType isEqualToString:@"1"] || [self.strOpneInvoiceType isEqualToString:@"2"]) {
//
//        if ([self.strReceiverUserCode isEqualToString:@""]) {
//           [CJAlert showStringDismissWithDelay:@"请选择实际收票人"];
//            return;
//        }
//    }
    
    if ([CJControl getIsEmpty:self.fieldInvoiceHeader.text]) {
        [[[AFViewShaker alloc] initWithView:self.fieldInvoiceHeader] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:self.fieldInvoiceHeader.placeholder];
            
        }];
    }else if ([CJControl getIsEmpty:self.fieldTaxNum.text]) {
        [[[AFViewShaker alloc] initWithView:self.fieldTaxNum] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:self.fieldTaxNum.placeholder];
            
        }];
    }else if ([CJControl getIsEmpty:self.strBankCode]) {
//        [[[AFViewShaker alloc] initWithView:self.fieldBankName] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:@"请选择基本开户行名称"];
//        }];
    }else if ([CJControl getIsEmpty:self.fieldBankIDNum.text]) {
        [[[AFViewShaker alloc] initWithView:self.fieldBankIDNum] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:self.fieldBankIDNum.placeholder];
            
        }];
    }else if ([CJControl getIsEmpty:self.fieldRegisterAddr.text]) {
        [[[AFViewShaker alloc] initWithView:self.fieldRegisterAddr] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:self.fieldRegisterAddr.placeholder];
            
        }];
    }else if ([CJControl getIsEmpty:self.fieldRegisterPhone.text]) {
        [[[AFViewShaker alloc] initWithView:self.fieldRegisterPhone] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:self.fieldRegisterPhone.placeholder];
            
        }];
    }else{
        
        NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/invoiceMessage",CJBaseUrl);
        NSDictionary *dicPar ;
        
        WS(ws);
        if (self.isChange) {
            dicPar= @{
                @"invoiceTitle":[CJControl getThreeUnaryValueStr:self.fieldInvoiceHeader.text],
                @"taxRegistrationNumber":[CJControl getThreeUnaryValueStr:self.fieldTaxNum.text],
                @"bank":self.strBankCode,
                @"accountNumber":[CJControl getThreeUnaryValueStr:self.fieldBankIDNum.text],
                @"registeredAddress":[CJControl getThreeUnaryValueStr:self.fieldRegisterAddr.text],
                @"registeredFixedPhone":[CJControl getThreeUnaryValueStr:self.fieldRegisterPhone.text],
    //            @"type":@"" , //self.strOpneInvoiceType,
    //            @"receiverUserCode":@"" ,//self.strReceiverUserCode,
                @"isInvoiceDefault":self.strSelect,
                @"code":self.strCode,
    //            @"receiver":@"" //[CJControl getThreeUnaryValueStr:ws.fieldReceiverUser.text]
            };
        
            [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PutType url:strURL parameters:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
                if (ws.blockSuccess) {
                    ws.blockSuccess(@"");
                    [ws pop];
                }
            } failure:^(NSError * _Nonnull error) {
                
            }];
            
        }else{
            dicPar= @{@"invoiceTitle":[CJControl getThreeUnaryValueStr:self.fieldInvoiceHeader.text],
                     @"taxRegistrationNumber":[CJControl getThreeUnaryValueStr:self.fieldTaxNum.text],
                     @"bank":self.strBankCode,
                     @"accountNumber":[CJControl getThreeUnaryValueStr:self.fieldBankIDNum.text],
                     @"registeredAddress":[CJControl getThreeUnaryValueStr:self.fieldRegisterAddr.text],
                     @"registeredFixedPhone":[CJControl getThreeUnaryValueStr:self.fieldRegisterPhone.text],
    //                 @"type": @"",//self.strOpneInvoiceType,
    //                 @"receiverUserCode":@"",//self.strReceiverUserCode,
                     @"isInvoiceDefault":self.strSelect,
    //                 @"receiver":@""//[CJControl getThreeUnaryValueStr:ws.fieldReceiverUser.text]
                    };
            
            [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:strURL parameters:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
                if (ws.blockSuccess) {
                    ws.blockSuccess(@"");
                    [ws pop];
                }
            } failure:^(NSError * _Nonnull error) {
                
            }];
        }
    }
}

@end
